<template>
  <vue-scroll class="page-building">
    <div class="page-header card-base header-accent">
      <h1>
        <span>设施列表</span>
        <div class="mt-10">
          <el-button type="primary" @click="dialogFormVisible = true">
            <i class="mdi mdi-plus"></i> 新增
          </el-button>
        </div>
      </h1>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }">
          <i class="mdi mdi-home-outline"></i>
        </el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: '/ecommerce' }">设施</el-breadcrumb-item>
        <el-breadcrumb-item>列表</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <el-row>
      <el-col>
        <div class="table-box card-base card-shadow--medium">
          <el-table
            :data="tableData.slice((currentPage-1)*pagesize,currentPage*pagesize)"
            style="width: 100%"
          >
            <el-table-column prop="name" label="名称" sortable width="280"></el-table-column>
            <el-table-column prop="createdtime" label="建立日期">
              <template slot-scope="scope">{{scope.row.createdtime | date}}</template>
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="200">
              <template slot-scope="scope">
                <el-button @click="deleteClick(scope.row)" type="text" size="small">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            class="paging"
            background
            :current-page="currentPage"
            layout="prev, pager, next"
            :total="total"
            @current-change="currentChange"
          ></el-pagination>
        </div>
      </el-col>
    </el-row>

    <el-dialog
      title="新增设施"
      :visible.sync="dialogFormVisible"
      :close-on-click-modal="false"
      @close="closeDialog"
      width="35%"
    >
      <el-form :model="form">
        <el-form-item label="名字" :label-width="formLabelWidth">
          <el-input
            name="name"
            v-model="form.name"
            v-validate="'required'"
            data-vv-as="名字"
            auto-complete="off"
          ></el-input>
          <span class="error">{{ errorBags.first('name')}}</span>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="createClick">提交</el-button>
      </span>
    </el-dialog>
  </vue-scroll>
</template>

<script>
import axios from "axios";
export default {
  name: "meetingroom",
  beforeRouteEnter(to, from, next) {
    next();
  },
  data() {
    return {
      tableData: [],
      total: 0,
      pagesize: 10,
      currentPage: 1,
      dialogFormVisible: false,
      form: {
        name: "",
        address: ""
      },
      formLabelWidth: "120px"
    };
  },
  methods: {
    deleteClick(row) {
      this.$confirm("此操作会永久删除此设施，确定继续?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true
      })
        .then(() => {
          axios
            .post("/api/facility/Remove/" + row.id)
            .then(response => {
              this.tableData = response.data.data;
              this.total = response.data.data.length;
              this.currentPage = 1;
              this.$message({
                type: "success",
                message: "删除成功"
              });
            })
            .catch(ex => {
              console.log(ex);
            });
        })
        .catch(ex => {
          console.log(ex);
        });
    },
    currentChange: function(currentPage) {
      this.currentPage = currentPage;
    },
    createClick() {
      this.$validator.validate().then(valid => {
        if (!valid) {
          // do stuff if not valid.
          return;
        } else {
          axios
            .post("/api/facility", this.form)
            .then(response => {
              this.dialogFormVisible = false;
              this.tableData = response.data.data;
              this.total = response.data.data.length;
              this.currentPage = 1;
              this.$message({
                type: "success",
                message: "建立成功"
              });
            })
            .catch(ex => {
              console.log(ex);
            });
        }
      });
    },
    closeDialog() {
      this.form.name = "";
      this.form.address = "";
      this.errorBags.clear();
    }
  },
  created() {
    axios.get("/api/facility").then(response => {
      this.tableData = response.data.data;
      this.total = response.data.data.length;
      this.currentPage = 1;
    });
  },
  watch: {
    // 如果路由发生变化，再次执行该方法
    $route(to, from) {
      // react to route changes...
    }
  }
};
</script>

<style lang="scss" scoped>
@import "../../../assets/scss/_variables";
</style>

<style lang="scss">
.el-table .cell {
  padding-left: 25px;
}
.page-building {
  .page-header {
    margin-bottom: 8px;
  }
  .el-form-item {
    margin-bottom: 16px;
  }
  .el-table th,
  .el-table td {
    padding: 5px 0;
  }
  .el-form-item__content {
    line-height: 14px;
  }
  .error {
    color: #d7195d;
    font-size: 12px;
    line-height: 1;
    padding-top: 4px;
  }
  h1 {
    button {
      float: right;
      padding: 10px 25px;
      background: rgba(255, 255, 255, 0.2);
      text-transform: uppercase;
      border: none;
      outline: none;
      cursor: pointer;
      color: white;
      opacity: 0.8;
      font-family: inherit;

      i {
        position: relative;
        top: 1px;
      }

      &:hover {
        opacity: 1;
      }
    }
  }
  .table-box {
    overflow: auto;
  }
  .paging {
    float: right;
    padding-right: 40px;
    margin-top: 10px;
    margin-bottom: 10px;
  }
}

@media (max-width: 768px) {
  .page-building {
    .vb-content {
      padding: 0 5px !important;
      margin: -5px;
      width: calc(100% + 10px) !important;
    }
  }
}
</style>


